package com.zcl.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class myBatisUtil {
    //获取数据库连接工厂
    public static SqlSessionFactory getSSF() throws IOException {
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }


    //得到sql链接对象
    public static SqlSession getSS( boolean autoCommint){
        try {
            //得到sqlSessionFactory对象
            SqlSessionFactory sqlSessionFactory=getSSF();
            //得到链接对象
            return sqlSessionFactory.openSession(autoCommint);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
